<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <base data-ice="baseUrl" href="../../../../">
  <title data-ice="title">src/module/date/strToDate.js | rx-util</title>
  <link type="text/css" rel="stylesheet" href="css/style.css">
  <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css">
  <script src="script/prettify/prettify.js"></script>
  <script src="script/manual.js"></script>
<meta name="description" content="rxliuli &#x4E2A;&#x4EBA;&#x4F7F;&#x7528;&#x7684; js &#x5DE5;&#x5177;&#x5E93;&#xFF08;&#x4EC5;&#x9002;&#x7528;&#x4E8E;&#x6D4F;&#x89C8;&#x5668;&#xFF09;"><meta property="twitter:card" content="summary"><meta property="twitter:title" content="rx-util"><meta property="twitter:description" content="rxliuli &#x4E2A;&#x4EBA;&#x4F7F;&#x7528;&#x7684; js &#x5DE5;&#x5177;&#x5E93;&#xFF08;&#x4EC5;&#x9002;&#x7528;&#x4E8E;&#x6D4F;&#x89C8;&#x5668;&#xFF09;"></head>
<body class="layout-container" data-ice="rootContainer">

<header>
  <a href="./">Home</a>
  
  <a href="identifiers.html">Reference</a>
  <a href="source.html">Source</a>
  <a href="test.html" data-ice="testLink">Test</a>
  <div class="search-box">
  <span>
    <img src="./image/search.png">
    <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span>
  </span>
    <ul class="search-result"></ul>
  </div>
</header>

<nav class="navigation" data-ice="nav"><div>
  <ul>
    
  <li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#module-ajax">module/ajax</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/module/ajax/FetchLimiting.js~FetchLimiting.html">FetchLimiting</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-download">download</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-downloadString">downloadString</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-downloadUrl">downloadUrl</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-fetchTimeout">fetchTimeout</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getCookies">getCookies</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-loadResource">loadResource</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-parseUrl">parseUrl</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-readLocal">readLocal</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-spliceParams">spliceParams</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-strToArrayBuffer">strToArrayBuffer</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#module-array">module/array</a><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-arrayToMap">arrayToMap</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-asIterator">asIterator</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-asyncFlatMap">asyncFlatMap</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-flatMap">flatMap</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-groupBy">groupBy</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-range">range</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-toObject">toObject</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-uniqueBy">uniqueBy</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#module-date">module/date</a><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-dateFormat">dateFormat</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-strToDate">strToDate</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#module-dom">module/dom</a><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-copyText">copyText</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-createElByString">createElByString</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getCusorPostion">getCusorPostion</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getSelectText">getSelectText</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-insertText">insertText</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isEditable">isEditable</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-lastFocus">lastFocus</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-removeEl">removeEl</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-removeText">removeText</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-setCusorPostion">setCusorPostion</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#module-event">module/event</a><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-watchEventListener">watchEventListener</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#module-formdata">module/formdata</a><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-appends">appends</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-deletes">deletes</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-formDataToArray">formDataToArray</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-objToFormData">objToFormData</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-sets">sets</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#module-function">module/function</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/module/function/StateMachine.js~StateMachine.html">StateMachine</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-debounce">debounce</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-returnItself">returnItself</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-safeExec">safeExec</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-singleModel">singleModel</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-throttle">throttle</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-timing">timing</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-wait">wait</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-waitResource">waitResource</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-watch">watch</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-watchObject">watchObject</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#module-number">module/number</a><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-randomInt">randomInt</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#module-obj">module/obj</a><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-blankToNullField">blankToNullField</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-emptyAllField">emptyAllField</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-excludeFields">excludeFields</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-mapToObject">mapToObject</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#module-string">module/string</a><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-blankToNull">blankToNull</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-fill">fill</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-format">format</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isFloat">isFloat</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isNumber">isNumber</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-toLowerCase">toLowerCase</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-toUpperCase">toUpperCase</a></span></span></li>
</ul>
</div>
</nav>

<div class="content" data-ice="content"><h1 data-ice="title">src/module/date/strToDate.js</h1>
<pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">// @ts-check

import { fill } from &apos;../string/fill&apos;
import { arrayToMap } from &apos;./../array/arrayToMap&apos;

/**
 * &#x65E5;&#x671F;&#x683C;&#x5F0F;&#x5316;&#x7C7B;
 */
class DateFormat {
  /**
   * &#x6784;&#x9020;&#x51FD;&#x6570;
   * @param {String} name &#x65E5;&#x671F;&#x683C;&#x5F0F;&#x7684;&#x540D;&#x79F0;
   * @param {String} format &#x65E5;&#x671F;&#x7684;&#x683C;&#x5F0F;&#x503C;
   * @param {String} value &#x683C;&#x5F0F;&#x5316;&#x5F97;&#x5230;&#x7684;&#x503C;
   * @param {Number} index &#x9700;&#x8981;&#x66FF;&#x6362;&#x4F4D;&#x7F6E;&#x7684;&#x7D22;&#x5F15;
   */
  constructor (name, format, value, index) {
    this.name = name
    this.format = format
    this.value = value
    this.index = index
  }
}

// &#x65E5;&#x671F;&#x65F6;&#x95F4;&#x7684;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;
const dateFormats = {
  year: &apos;y{4}|y{2}&apos;,
  month: &apos;M{1,2}&apos;,
  day: &apos;d{1,2}&apos;,
  hour: &apos;h{1,2}&apos;,
  minute: &apos;m{1,2}&apos;,
  second: &apos;s{1,2}&apos;,
  milliSecond: &apos;S{1,3}&apos;
}

/**
 * &#x89E3;&#x6790;&#x5B57;&#x7B26;&#x4E32;&#x4E3A; Date &#x5BF9;&#x8C61;
 * @param {String} dateStr &#x65E5;&#x671F;&#x5B57;&#x7B26;&#x4E32;
 * @param {String} fmt &#x65E5;&#x671F;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x683C;&#x5F0F;
 * &#x76EE;&#x524D;&#x4EC5;&#x652F;&#x6301;&#x4F7F;&#x7528; y(&#x5E74;),M(&#x6708;),d(&#x65E5;),h(&#x65F6;),m(&#x5206;),s(&#x79D2;),S(&#x6BEB;&#x79D2;)
 * @returns {Date} &#x89E3;&#x6790;&#x5F97;&#x5230;&#x7684; Date &#x5BF9;&#x8C61;
 */
export function strToDate (dateStr, fmt) {
  const now = new Date()
  // &#x5982;&#x679C;&#x6CA1;&#x6709;&#x683C;&#x5F0F;&#x5316;&#x67D0;&#x9879;&#x7684;&#x8BDD;&#x5219;&#x8BBE;&#x7F6E;&#x4E3A;&#x9ED8;&#x8BA4;&#x65F6;&#x95F4;
  const defaultDateValues = {
    year: now.getFullYear().toString(),
    month: &apos;01&apos;,
    day: &apos;01&apos;,
    hour: &apos;00&apos;,
    minute: &apos;00&apos;,
    second: &apos;00&apos;,
    milliSecond: &apos;000&apos;
  }
  // &#x4FDD;&#x5B58;&#x5BF9;&#x4F20;&#x5165;&#x7684;&#x65E5;&#x671F;&#x5B57;&#x7B26;&#x4E32;&#x8FDB;&#x884C;&#x683C;&#x5F0F;&#x5316;&#x7684;&#x5168;&#x90E8;&#x4FE1;&#x606F;&#x6570;&#x7EC4;&#x5217;&#x8868;
  const dateUnits = []
  for (const fmtName in dateFormats) {
    const regExp = new RegExp(dateFormats[fmtName])
    if (regExp.test(fmt)) {
      const matchStr = regExp.exec(fmt)[0]
      const regexStr = fill(&apos;`&apos;, matchStr.length)
      const index = fmt.indexOf(matchStr)
      fmt = fmt.replace(matchStr, regexStr)
      dateUnits.push(
        new DateFormat(fmtName, fill(&apos;\\d&apos;, matchStr.length), null, index)
      )
    } else {
      dateUnits.push(
        new DateFormat(fmtName, null, defaultDateValues[fmtName], -1)
      )
    }
  }
  // &#x8FDB;&#x884C;&#x9A8C;&#x8BC1;&#x662F;&#x5426;&#x771F;&#x7684;&#x662F;&#x7B26;&#x5408;&#x4F20;&#x5165;&#x683C;&#x5F0F;&#x7684;&#x5B57;&#x7B26;&#x4E32;
  fmt = fmt.replace(new RegExp(&apos;`&apos;, &apos;g&apos;), &apos;\\d&apos;)
  if (!new RegExp(`^${fmt}$`).test(dateStr)) {
    return null
  }
  // &#x8FDB;&#x884C;&#x4E00;&#x6B21;&#x6392;&#x5E8F;, &#x4F9D;&#x6B21;&#x5BF9;&#x5B57;&#x7B26;&#x4E32;&#x8FDB;&#x884C;&#x622A;&#x53D6;
  dateUnits
    // &#x8FC7;&#x6EE4;&#x6389;&#x6CA1;&#x6709;&#x5F97;&#x5230;&#x683C;&#x5F0F;&#x5316;&#x7684;&#x5BF9;&#x8C61;
    .filter(({ format }) =&gt; format)
    // &#x6309;&#x7167;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x65E5;&#x671F;&#x7247;&#x6BB5;&#x7684;&#x7D22;&#x5F15;&#x8FDB;&#x884C;&#x6392;&#x5E8F;
    .sort(function (a, b) {
      return a.index - b.index
    })
    // &#x83B7;&#x53D6;&#x5230;&#x5339;&#x914D;&#x7684;&#x65E5;&#x671F;&#x7247;&#x6BB5;&#x7684;&#x503C;
    .map(format =&gt; {
      const matchDateUnit = new RegExp(format.format).exec(dateStr)
      if (matchDateUnit !== null &amp;&amp; matchDateUnit.length &gt; 0) {
        dateStr = dateStr.replace(matchDateUnit[0], &apos;&apos;)
        format.value = matchDateUnit[0]
      }
      return format
    })
    // &#x8986;&#x5199;&#x5230; dateStr &#x4E0A;&#x9762;
    .forEach(({ format }, i) =&gt; {
      const matchDateUnit = new RegExp(format).exec(dateStr)
      if (matchDateUnit !== null &amp;&amp; matchDateUnit.length &gt; 0) {
        dateStr = dateStr.replace(matchDateUnit[0], &apos;&apos;)
        dateUnits[i].value = matchDateUnit[0]
      }
    })
  // &#x5C06;&#x622A;&#x53D6;&#x5B8C;&#x6210;&#x7684;&#x4FE1;&#x606F;&#x5C01;&#x88C5;&#x6210;&#x5BF9;&#x8C61;&#x5E76;&#x683C;&#x5F0F;&#x5316;&#x6807;&#x51C6;&#x7684;&#x65E5;&#x671F;&#x5B57;&#x7B26;&#x4E32;
  const map = arrayToMap(dateUnits, item =&gt; item.name, item =&gt; item.value)
  if (map.get(&apos;year&apos;).length === 2) {
    map.set(
      &apos;year&apos;,
      defaultDateValues.year.substr(0, 2).concat(map.get(&apos;year&apos;))
    )
  }
  // &#x6CE8;&#x610F;&#xFF1A;&#x6B64;&#x5904;&#x4F7F;&#x7528;&#x7684;&#x662F;&#x672C;&#x5730;&#x65F6;&#x95F4;&#x800C;&#x975E; UTC &#x65F6;&#x95F4;
  const date = `${map.get(&apos;year&apos;)}-${map.get(&apos;month&apos;)}-${map.get(
    &apos;day&apos;
  )}T${map.get(&apos;hour&apos;)}:${map.get(&apos;minute&apos;)}:${map.get(&apos;second&apos;)}.${map.get(
    &apos;milliSecond&apos;
  )}`
  return new Date(date)
}
</code></pre>

</div>

<footer class="footer">
  Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(1.1.0)</span><img src="./image/esdoc-logo-mini-black.png"></a>
</footer>

<script src="script/search_index.js"></script>
<script src="script/search.js"></script>
<script src="script/pretty-print.js"></script>
<script src="script/inherited-summary.js"></script>
<script src="script/test-summary.js"></script>
<script src="script/inner-link.js"></script>
<script src="script/patch-for-local.js"></script>
</body>
</html>
